



#include "disp_manager.h"
#include <linux/kernel.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>



//管理底层的LCD、WEB

static display_operationp g_dispdevs = NULL;
static display_operationp g_dispdevfault = NULL;
static DispBuff           g_tDispBuff ;




static int line_width  ;
static int pixel_width  ;


//绘制像素
void putpixel(int x, int y ,unsigned int color_double)
{
	//基地址+第几行+第几个像素，找到对应字节，填充像素
	unsigned char *pen_8 = g_tDispBuff.buff + y*line_width + x*pixel_width;
	unsigned short *pen_16; 
	unsigned int *pen_32;	

	unsigned int red, green, blue;	

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;
	

	switch (g_tDispBuff.b_p)
	{
		case 8:
		{
			*pen_8 = color_double;
			break;
		}
		case 16:
		{
			/* 565 */
			red   = (color_double >> 16) & 0xff;
			green = (color_double >> 8) & 0xff;
			blue  = (color_double >> 0) & 0xff;
			color_double = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color_double;
			break;
		}
		case 32:
		{
			*pen_32 = color_double;
			break;
		}
		default:
		{
			printf("can't surport %dbpp\n", g_tDispBuff.b_p);
			break;
		}
	}





}



void DrawFontBitMap(fontbitmap_p Fontbitmap_p, unsigned int color_double)
{


	int	i, j, p, q;


int x=Fontbitmap_p->tRegion.leftup_x;
int y=Fontbitmap_p->tRegion.leftup_y;
int x_max = x + Fontbitmap_p->tRegion.width;
int y_max = y + Fontbitmap_p->tRegion.heigh;


unsigned char *buffer = Fontbitmap_p->bitmapbuff;

   for ( j = y, q = 0; j < y_max; j++, q++ )
   {
	   for ( i = x, p = 0; i < x_max; i++, p++ )
	   {
		   if ( i < 0	   || j < 0 	  ||  i >= g_tDispBuff.xres  || j >= g_tDispBuff.yres )
		   {
		  			 continue;
		   }

			if(buffer[q * Fontbitmap_p->tRegion.width + p])
			{
		   		putpixel(i, j, color_double);
			}
	   }
   }






}




void DrawRegion(Region_p pRegion, unsigned int color_double)
{

	int x = pRegion->leftup_x;
	int y = pRegion->leftup_y;
	int width = pRegion->width;
	int heigh = pRegion->heigh;

	int i,j;

	for(j=y;j<(y+heigh);j++)
	{
		for(i=x;i<(x+width);i++)
		{	//printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

			 if ( i < 0	   || j < 0 	  ||  i >= g_tDispBuff.xres  || j >= g_tDispBuff.yres )
		   {
		  			 continue;
		   }
	
		   putpixel(i, j, color_double);

		
		}



	}





}

void DrawTextInRegionCentral( char *name , Region_p pRegion, unsigned int color_double)
{

	fontbitmap Fontbitmap;

	int i =0 ;

	int len = strlen(name);

	int fontsize = pRegion->width / 2 /len; 

	int Origanx,Origany;

	if(fontsize > pRegion->heigh)
	{
		fontsize	= pRegion->heigh;
	}
	printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

	Origanx = (pRegion->width - len * fontsize) / 2 + pRegion->leftup_x;
	Origany = (pRegion->heigh -  fontsize) / 2 +fontsize+ pRegion->leftup_y;
	printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);


	printf("fontsize %d \n", fontsize);

	SetFontSize(fontsize);
	
	printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	while(name[i])
	{

		
		/*获得位图*/
		Fontbitmap.CurOrigin_x = Origanx;
		Fontbitmap.CurOrigin_y = Origany;
		if(GetFontBitMap(name[i], &Fontbitmap))
		{
			printf("GetFontBitMap error \n");

		}

		/*画到buff*/
		DrawFontBitMap(&Fontbitmap, color_double);

		
		Origanx = Fontbitmap.NexOrigin_x;
		Origany = Fontbitmap.NexOrigin_y;

		i++;

		
	}

	


}





void lcd_clear(void)
{
	size_t count ;
	count = g_tDispBuff.xres * g_tDispBuff.yres *g_tDispBuff.b_p /8;

	memset(g_tDispBuff.buff,0xff,count);


}


void RegsiterDisplay(display_operationp display_operation_p)
{

	display_operation_p->ptNEXT = g_dispdevs;

	g_dispdevs = display_operation_p;



}


int selectdefaulterDisplay(char * name)
{
	display_operationp ptmp = g_dispdevs;

	while(ptmp)
	{
		
		if(ptmp->name == name)
		{
			g_dispdevfault = ptmp;
			return 0;
		}
		ptmp = ptmp->ptNEXT;
	}
	return -1;

}



//初始化显示器
int initdefaulterDisplay(void)
{
	int ret;
	ret = g_dispdevfault->DeviceInit();
	if(ret)
	{
		printf("DeviceInit err\n");
		return -1;


	}
	ret = g_dispdevfault->getbuff(&g_tDispBuff);
		
	if(ret)
	{
		printf("getbuff err\n");
		return -1;


	}

//一行多少像素 * 一个像素多少位 /8 = 一行多少字节
	line_width  = g_tDispBuff.xres * g_tDispBuff.b_p /8;

//每个像素的宽度（/字节）
	pixel_width = g_tDispBuff.b_p /8;




	return 0;




}


DispBuff_p get_initdisbuffer(void)
{


	return &g_tDispBuff;


}









//刷到硬件
int FlushDisplayRegion(Region_p Regionp, DispBuff disbuff)
{
	return  g_dispdevfault->FLushRegion(Regionp, disbuff);
}



void DisplayInit(void )
{


	FramebufferInit();


}
















